<template lang="pug">
.detail
  .detail-header
    .detail-header-btn(@click="$router.back()")
      .iconfont.icon-back
      .detail-header-text 样品详情
  .detail-content
    .detail-info
      .detail-preview
        .detail-preview-img(:style="`background-image: url(${previewImg})`" @click="previewImage")
          .detail-preview-tag(v-if="detail.recommend")
            .preview-tag 推荐
        .detail-preview-list
          .detail-img-list-bd
            .detail-select-img(v-for="item in detail.fileUrlList" :class="{ 'active': previewImg === item }" :style="`background-image: url(${item})`" @click="selectPreview(item)")
        .detail-info-content
          .detail-name {{detail.sampleName}}
          .detail-price {{detail.specCount.minMarketPrice === detail.specCount.maxMarketPrice ? detail.specCount.minMarketPrice : '￥' + detail.specCount.minMarketPrice + ' ~ ￥' + detail.specCount.maxMarketPrice}} / {{detail.specCount.unit}}
          .detail-category.detail-info-item {{detail.sampleCategoryNamePath}}
          .detail-brand.detail-info-item 品牌：{{detail.sampleBrandsName}}
          .detail-place.detail-info-item 产地：{{detail.prodPlaceName}}
          .detail-delivery.detail-info-item 供货周期：{{detail.sampleDeliveryCycle}}
          .detail-new.iconfont.icon-new(v-if="detail.newFlag")
        .detail-info-handles
          .iconfont.icon-add(@click="addProject")
          .iconfont.icon-collection(@click="changeCollection")
    .detail-params
      .detail-spec
        .spec-item(v-for="item in detail.specAttrList")
          .spec-item-label {{item.name}}
          .spec-item-value {{item.values}}
      .detail-supplier
        .supplier-left
          .supplier-logo
            img(:src="detail.supplier.supplierLogoUrl")
          .supplier-name {{detail.supplier.supplierName}}
        .supplier-right
          .supllier-info-item TEL：{{detail.supplier.supplierPhone}}
          .supllier-info-item NAME：{{detail.supplier.supplierPrincipal}}
          .supllier-info-item ADD：{{detail.supplier.address}}
      .detail-attr-container
        .detail-attr
          .attr-item(v-for="item, index in detail.sampleAttrList" v-if="index < 6")
            .attr-item-label {{item.attributeName}}
            .attr-item-value {{item.attributeValues}}
        .detail-attr-more(v-if="detail.sampleAttrList.length > 6")
          .more-btn(@click="showMoreAttr = true") 查看更多>>
      .detail-img
        img(v-for="item in detail.detailFileUrlList" :src="item")
      .detail-case-list
        .case-list-title 相关项目
        .case-list
          .case-item(v-for="item in detail.linkCaseList")
            .case-img(v-if="!item.type" :style="`background-image: url(${item.picImg})`")
            video.case-video(v-else :src="item.picImg")
            .case-item-bd
              .case-info-header
                .case-name {{item.projectName}}
              .case-detail {{item.projectDetail}}
  attr-modal(:show="showMoreAttr" :attrs="detail.sampleAttrList" @close="closeAttrModal")
</template>

<script>
import { getSampleDetail, markMaterial } from 'api'
import AttrModal from '@/components/attrModal'
import { mapActions } from 'vuex'
export default {
  name: 'detail',
  data () {
    return {
      detail: {
        specCount: {},
        supplier: {},
        sampleAttrList: []
      },
      previewImg: '',
      showMoreAttr: false
    }
  },
  components: {
    AttrModal
  },
  methods: {
    ...mapActions([
      'getUserInfo',
      'changeProjectModal',
      'changeCarousel'
    ]),
    previewImage () {
      this.changeCarousel({ show: true, list: this.detail.fileUrlList })
    },
    closeAttrModal () {
      this.showMoreAttr = false
    },
    async addProject () {
      try {
        await this.getUserInfo()
        this.changeProjectModal({ show: true, materialDetail: this.detail })
      } catch (err) {
        this.message({ show: true, content: err.message || err.ResultSet.errors })
        console.log(err)
      }
    },
    async changeCollection () {
      try {
        await this.getUserInfo()
        if (this.locking) return
        this.locking = true
        await this.$axios.post(markMaterial, { materialId: this.detail.materialId })
        this.locking = false
        this.message({ show: true, content: '收藏成功！', type: 'success' })
      } catch (err) {
        this.locking = false
        this.message({ show: true, content: err.message || err.ResultSet.errors })
        console.log(err)
      }
    },
    selectPreview (item) {
      this.previewImg = item
    },
    async getDetail () {
      try {
        const { ResultSet } = await this.$axios.post(getSampleDetail, { materialsSampleId: this.$route.params.id })
        this.detail = ResultSet.userData.detail
        if (this.detail.fileUrlList.length) {
          this.previewImg = this.detail.fileUrlList[0]
        }
      } catch (err) {
        this.message({ show: true, content: err.message || err.ResultSet.errors })
        console.log(err)
      }
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {
      vm.getDetail()
    })
  }
}
</script>

<style lang="scss" scoped>
.detail {
  height: 100%;
}
.detail-header {
  display: flex;
  align-items: center;
  font-size: 22px;
  padding: 0 20px;
  height: 24px;
  font-weight: bold;
  color: #CD9E78;
  .detail-header-btn {
    display: flex;
    align-items: center;
  }
  .iconfont {
    margin-right: 10px;
  }
}
.detail-content {
  height: calc(100% - 24px);
  display: flex;
  padding: 20px 40px;
}
.detail-info {
  min-width: 400px;
  width: 400px;
}
.detail-preview-img {
  position: relative;
  width: 400px;
  height: 250px;
  background-position: center;
  background-repeat: no-repeat;
  background-size: contain;
  border: 1px solid #ddd;
  .icon-recom {
    position: absolute;
    top: 0;
    left: 0;
    font-size: 60px;
    z-index: 1;
    color: #E98F36;
  }
}
.detail-preview-list {
  max-width: 400px;
  padding: 20px 0 10px;
  overflow: auto;
}
.detail-img-list-bd {
  width: 100%;
  display: flex;
  align-items: center;
}
.detail-select-img {
  width: 100px;
  min-width: 100px;
  height: 80px;
  margin-right: 10px;
  background-position: center;
  background-repeat: no-repeat;
  background-size: contain;
  border: 2px solid transparent;
  &.active {
    border-color: #CD9E78;
  }
}
.detail-info-content {
  position: relative;
}
.detail-name {
  font-size: 20px;
  color: #363636;
  margin-top: 10px;
  font-weight: bold;
  display: -webkit-box;
  overflow: hidden;
  text-overflow: ellipsis;
  word-wrap: break-word;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}
.detail-price {
  font-size: 18px;
  margin-top: 10px;
  font-weight: bold;
  color: #CD9E78;
}
.detail-info-item {
  font-size: 16px;
  line-height: 30px;
  color: #666;
}
.detail-new {
  position: absolute;
  right: 0;
  bottom: 0;
  font-size: 40px;
  color: #E98F36;
}
.detail-preview-tag {
  width: 200px;
  height: 80px;
  overflow: hidden;
  .preview-tag {
    transform: rotate(-45deg) translate(-20px,-50px);
    width: 100%;
    height: 30px;
    padding-left: 45px;
    line-height: 30px;
    font-size: 16px;
    color: #E1D8CA;
    font-weight: bold;
    background: #E98F36;
  }
}
.detail-info-handles {
  margin: 20px 0;
  display: flex;
  align-items: center;
  justify-content: center;
  .iconfont {
    margin: 0 30px;
    font-size: 30px;
  }
  .icon-add {
    color: #CD9E78;
  }
}
.detail-params {
  height: 100%;
  overflow: auto;
  flex: 1;
  padding-left: 60px;
}
.detail-spec {
  display: flex;
  flex-wrap: wrap;
  border-bottom: 1px solid #ccc;
  .spec-item {
    width: 50%;
    padding: 10px 0;
    .spec-item-label {
      font-size: 14px;
      color: #999;
    }
    .spec-item-value {
      font-size: 16px;
      font-weight: bold;
      margin-top: 10px;
    }
  }
}
.detail-supplier {
  display: flex;
  padding: 20px 0;
}
.supplier-left,
.supplier-right {
  width: 50%;
}
.supplier-left {
  padding: 0 10px
}
.supplier-logo {
  width: 60px;
  height: 60px;
}
.supplier-name {
  margin-top: 10px;
  font-size: 14px;
  font-weight: bold;
  color: #CD9E78;
}
.supllier-info-item {
  font-size: 14px;
  font-weight: bold;
  color: #CD9E78;
  line-height: 28px;
}
.detail-attr-container {
  border-bottom: 1px solid #ccc;
}
.detail-attr {
  display: flex;
  flex-wrap: wrap;
  .attr-item {
    width: 50%;
    padding: 10px 0;
    .attr-item-label {
      font-size: 14px;
      color: #999;
    }
    .attr-item-value {
      font-size: 16px;
      font-weight: bold;
      margin-top: 10px;
    }
  }
}
.detail-attr-more {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding-bottom: 20px;
  .more-btn {
    font-size: 14px;
    color: #CD9E78;
  }
}
.detail-img {
  padding-top: 20px;
  img {
    width: 100%;
    margin-bottom: 20px;
  }
}
.detail-case-list {
  .case-list-title {
    font-size: 18px;
    font-weight: bold;
  }
  .case-list {
    margin-top: 20px;
  }
  .case-item {
    display: flex;
    margin-bottom: 15px;
    .case-img {
      width: 200px;
      height: 150px;
      background-position: center;
      background-repeat: no-repeat;
      background-size: cover
    }
    .case-video {
      width: 200px;
      height: 150px;
    }
    .case-item-bd {
      flex: 1;
      overflow: hidden;
      padding-left: 20px;
    }
    .case-info-header {
      padding-bottom: 10px;
      border-bottom: 1px solid #ccc;
    }
    .case-name {
      font-size: 16px;
      font-weight: bold;
      display: -webkit-box;
      overflow: hidden;
      text-overflow: ellipsis;
      word-wrap: break-word;
      -webkit-line-clamp: 2;
      -webkit-box-orient: vertical;
    }
    .case-detail {
      padding-top: 10px;
      font-size: 14px;
    }
  }
}
</style>